detectors: add Llama Guard shield verdict parsing - #2127
Conversation
Assisted-by: OpenAI Codex Signed-off-by: Imran Ahamed <immu4989@gmail.com>
|
Independent verification on Windows 11 / Python 3.11, against this branch at 321eb11: The parser handles the leading-whitespace case I mentioned when closing #2109 — Worth knowing that the same defect still stands in so a guard that pads its verdict reads as shields-down today. Nothing for this PR to do — your classes do their own parsing and are unaffected — but if #1893 lands, Two nits, neither blocking:
|
Assisted-by: OpenAI Codex Signed-off-by: Imran Ahamed <immu4989@gmail.com>
|
Both nits are addressed in ff12b0e; re-verified on Windows 11 / Python 3.11 at that commit: Parser behaviour is unchanged by the cleanup — spot-checked against the cases from my earlier comment: Dropping |
Adds
shields.LlamaGuardUpandshields.LlamaGuardDownfor targets that emit Llama Guard's published binary verdict format.LlamaGuardUptreatsunsafeas a fired shield.LlamaGuardDowntreatssafeas a quiet shield for known clean probes. Both read the first nonblank line as a standalone verdict, accept an optional category line afterunsafe, and store category codes per output inattempt.notes["llamaguard_hazard_categories"].The category codes remain unresolved because their meanings vary between Llama Guard versions. Malformed output and prose such as
safe to proceedare not accepted as verdicts. Missing outputs remain unscored.Closes #1191.
This follows @leondz's direction on #1191 that binary guard outputs belong in
shields, category results belong in notes, and the implementation should not prefer an endpoint vendor. Thanks to @Yigtwxx for sharing the leading whitespace and version dependent category findings after closing #2109.Duplicate work check
PR #2109 was closed after its author yielded #1191 to me. Before opening this PR, searches for
1191 in:bodyandLlamaGuardfound no other open implementation.Verification
.venv/bin/python -m pytest tests/detectors/test_detectors_shields.py -qResult: 15 passed.
.venv/bin/python -m pytest tests/detectors/ -qResult: 788 passed and 34 skipped.
.venv/bin/python -m pytest tests/detectors/test_detectors.py -q -k LlamaGuardResult: 8 passed.
.venv/bin/python -m pytest tests/plugins/test_plugin_load.py -q -k LlamaGuardResult: 2 passed.
.venv/bin/python -m pytest tests/test_docs.py -qResult: 682 passed.
.venv/bin/python -m black --config pyproject.toml --check garak/detectors/shields.py tests/detectors/test_detectors_shields.pyResult: both files unchanged.
.venv/bin/python -m garak --plugin_info detectors.shields.LlamaGuardUpResult: the detector loads and its metadata renders.
.venv/bin/python -m pytest tests/ -qResult: 5736 passed, 106 skipped, and 4 preexisting failures. The same four tests fail unchanged on untouched
origin/main: the Groq and LiteLLM tests passMessagewhere current generator code requiresConversation, and two AudioAchillesHeel tests lack the optionalsoundfileandlibrosadependencies.Positive cases cover safe and unsafe verdicts, leading blank lines, case variation, multiple category codes, Llama Guard 1
Ocodes, laterScodes, duplicate codes, and outputs without categories.Negative cases cover prose beginning with a verdict word, categories attached to
safe, malformed category lines, extra nonblank lines, empty text, and missing outputs.Documentation is provided through the shields module and class docstrings, which are included by the existing
automodulepage.AI assistance
AI assistance was used to develop this change. This PR remains a draft until I complete a line by line review of the two changed files and can explain the parser, score polarity, notes contract, and tests.